class code4 {
    //大根堆+小根堆
    PriorityQueue<Integer> left;
    PriorityQueue<Integer> right;
    public MedianFinder() {
        left = new PriorityQueue<>((a,b)->(b-a));
        right = new PriorityQueue<>();
    }

    public void addNum(int num) {
        if(left.size()==right.size()) {
            if(left.isEmpty()||num<=left.peek()) {
                left.offer(num);
            } else {
                right.offer(num);
                left.offer(right.poll());
            }
        } else {
            if(num<=left.peek()) {
                left.offer(num);
                right.offer(left.poll());
            } else {
                right.offer(num);
            }
        }
    }

    public double findMedian() {
        if(left.size()==right.size()) {
            return (left.peek()+right.peek())/2.0;
        } else {
            return left.peek();
        }
    }
}